*Load merged dataset 

use merged, clear

*Select Industry
di $ind
keep if CIIU3_3D == $ind

*Merge in firm-level price of top intermediate input
preserve
use "INP_P", clear
keep ISIC ID_FIRM CPC3_Input_Top_Firm unit_pr_firm_spec_top_input_v2 Unit_Input
sort ISIC ID_FIRM
rename ISIC CIIU3_3D
rename ID_FIRM firm_id
**Drop Extreme Outliers in Input Prices
drop if CPC3_Input_Top_Firm == 143 & unit_pr_firm > 100
drop if CIIU == 221 & CPC3_Input_Top_Firm == 145 & unit_pr_firm > 20
save input_prices, replace
restore
sort CIIU3_3D firm_id 
merge m:1 CIIU3_3D firm_id using "input_prices"
drop _m

*Compute firm price index
bys CIIU3_3D cpc3 CPC3_Input_Top_Firm Unit_Input_Top_Firm: egen double m_unit_pr_firm = median(unit_pr_firm)
gen double index_pr_firm = unit_pr_firm / m_unit_pr_firm

*Flag products with small number of observations (less than 40)
bys cpc3: egen prod_obs = count(year)
gen small_prod = 0
replace small_prod = 1 if prod_obs < 40

*Compute Fraction of Total Income from Sources other than Sales
gen double f1_add_rev = f1_oper_rev_it - f1_sale_it
replace f1_add_rev = 0 if f1_add_rev < 0 & f1_add_rev ~= .
gen double f1_pct_add_rev_it = f1_add_rev / f1_oper_rev_it

*Define quantity variables
gen double q_prod_ijtu = f3_q_equiv_prod_ijtu
gen double q_exp_ijtu = f3_q_equiv_exp_ijtu
gen double q_sale_ijt = f3_q_equiv_sale_ijtu

*Define exporting variable
gen exp_dummy = 0
foreach var of varlist fob_x_ijtu_*  {
   replace exp_dummy = 1 if `var' ~= . & `var' ~= 0
}
bys CIIU firm_id year cpc3: egen dexp_ftp = sum(exp_dummy)
replace dexp_ftp = 1 if dexp_ftp > 0 & dexp_ftp ~= .

*Define export quantity variables by destination
replace q_fob_x_ijtu = ad_q_equiv_exp_ijtu
#delimit ;
local countries "AFG AGO AIA ALB ANT ARG ARM ATG AUS AUT AZE BDI BEL BEN BGD BGR BHR 
                   BHS BIH BLR BLZ BMU BOL BRA BRB CAF CAN CHE CHN CIV CMR COD COG 
				   COL CPV CRI CUB CYP CZE DEU DMA DNK DOM DZA ECU EGY ESP EST ETH
				   FIN FJI FLK FRA FSM GAB GBR GEO GHA GIN GMB GNQ GRC GRD GRL GTM 
				   GUY HKG HND HRV HTI HUN IDN IND IRL IRN IRQ ISL ISR ITA JAM JOR
				   JPN KAZ KEN KGZ KHM KIR KNA KOR KWT LAO LBN LBR LBY LCA LKA LTU 
				   LUX LVA MAC MAR MCO MDA MDG MDV MEX MHL MKD MLI MLT MMR MNG MOZ 
				   MRT MUS MWI MYS NAM NER NGA NIC NLD NOR NPL NZL OMN PAK PAN PER 
				   PHL PNG POL PRK PRT PRY QAT ROU RUS SAU SDN SEN SGP SLB SLE SLV 
				   SMR SPM SRB STP SUR SVK SVN SWE SWZ SYC SYR TGO THA TJK TON TTO 
				   TUN TUR TUV TWN TZA UAE UGA UKR URY USA UZB VCT VEN VNM VUT WSM 
				   YEM ZAF ZMB ZWE
				   ABW COK CYM GUM MSR MTQ NCL PRI PYF TCA VGB VIR" ;
#delimit cr
foreach coun of local countries  {
capture replace q_fob_x_ijtu_`coun' = ad_q_equiv_exp_ijtu_`coun'
}

*Drop observations with zero produced output
drop if q_prod_ijtu == .  

*Harmonize ENIA and Customs data
replace fob_x_ijtu = . if q_exp_ijtu == 0
replace q_exp_ijtu = 0 if fob_x_ijtu == .

*Define additional variables
gen double q_prod_to_sold_ratio = q_prod_ijt / q_sale_ijt
gen double y_fjt = q_prod_ijt
gen double exp_ratio = q_exp_ijt / q_sale_ijt

*Compute export quantities for each destination
#delimit ;
local countries "AFG AGO AIA ALB ANT ARG ARM ATG AUS AUT AZE BDI BEL BEN BGD BGR BHR 
                   BHS BIH BLR BLZ BMU BOL BRA BRB CAF CAN CHE CHN CIV CMR COD COG 
				   COL CPV CRI CUB CYP CZE DEU DMA DNK DOM DZA ECU EGY ESP EST ETH
				   FIN FJI FLK FRA FSM GAB GBR GEO GHA GIN GMB GNQ GRC GRD GRL GTM 
				   GUY HKG HND HRV HTI HUN IDN IND IRL IRN IRQ ISL ISR ITA JAM JOR
				   JPN KAZ KEN KGZ KHM KIR KNA KOR KWT LAO LBN LBR LBY LCA LKA LTU 
				   LUX LVA MAC MAR MCO MDA MDG MDV MEX MHL MKD MLI MLT MMR MNG MOZ 
				   MRT MUS MWI MYS NAM NER NGA NIC NLD NOR NPL NZL OMN PAK PAN PER 
				   PHL PNG POL PRK PRT PRY QAT ROU RUS SAU SDN SEN SGP SLB SLE SLV 
				   SMR SPM SRB STP SUR SVK SVN SWE SWZ SYC SYR TGO THA TJK TON TTO 
				   TUN TUR TUV TWN TZA UAE UGA UKR URY USA UZB VCT VEN VNM VUT WSM 
				   YEM  ZAF ZMB ZWE
				   ABW COK CYM GUM MSR MTQ NCL PRI PYF TCA VGB VIR" ;
#delimit cr


foreach coun of local countries  {
capture gen double q_temp_`coun' = q_fob_x_ijtu_`coun' * (q_exp_ijt / q_fob_x_ijtu)
capture drop q_fob_x_ijtu_`coun'
rename q_temp_`coun' q_fob_x_ijtu_`coun'
}

*Compute Price Outliers
do outliers

* Compute Domestic Sales (in Value) of Produced Output
gen double nr_fjt_dom = ((f3_sale_ijt - fob_x_ijtu) * q_prod_to_sold_ratio) / (1-f1_pct_add_rev_it)
replace nr_fjt_dom = (f3_sale_ijt * q_prod_to_sold_ratio) / (1-f1_pct_add_rev_it) if fob_x_ijtu == .
replace nr_fjt_dom = . if nr_fjt_dom < 0

* Compute Total Sales (in Value) of Produced Output
gen double nr_fjt = (f3_sale_ijt * q_prod_to_sold_ratio) / (1- f1_pct_add_rev_it)
bys firm_id year: egen double nr_ft = sum(nr_fjt) 

* Compute Quantity of Exported Output and Quantity of Domestic Output
gen double y_fjt_exp_total = q_exp_ijtu * q_prod_to_sold_ratio 
replace y_fjt_exp_total = 0 if fob_x_ijtu == .
gen double y_fjt_dom = q_prod_ijt - (q_exp_ijtu * q_prod_to_sold_ratio)
replace y_fjt_dom = q_prod_ijt if fob_x_ijtu == . 

*Compute price variables
gen double p_fjt_dom = nr_fjt_dom / y_fjt_dom
replace p_fjt_dom = . if p_fjt_dom == 0
gen double p_tot = (f3_sale_ijt / q_sale_ijt) / (1 - f1_pct_add_rev_it)

*Compute Domestic Price Outliers
do outliers_dom

*Compute number of products
bys firm_id year: egen num_prod = count(cpc3)

*Create variables at the firm-time level (only used for single product firms)
bys firm_id year: egen double y_ft = sum(y_fjt)
gen double p_ft_dom = .
replace p_ft_dom = p_fjt_dom if num_prod == 1

*Deflate Revenues Using an Industry-Level Deflator
gen double rr_fjt = nr_fjt / (defl_prod_cpc5_v4 / 100)
gen double rr_ft = nr_ft / (defl_prod_cpc5_v4 / 100)

* Creates Real and Nominal Intermediate Inputs (and their components)
gen double ni = electr_pq_it + combus_pq_it + water_pq_it + f4_cons_input_it
gen ni_ratio_f1_f4 = (f1_pur_input_it + f1_tran_input_it) / f4_cons_input_it

gen double relec = electr_pq_it / (cpi_electr / 100)
gen double cpi_comb = (cpi_gas + cpi_other_comb) / 2
gen double rcomb = combus_pq_it / (cpi_comb / 100)
gen double rwater = water_pq_it / (cpi_water / 100)

gen double r_inputs_temp = (f4_cons_input_it) / (n_defl_inputs_4 / 100)
gen double r_inputs_alt = r_inputs_temp
gen double r_inputs = r_inputs_temp / index_pr_firm
gen int_input_price = f4_cons_input_it / r_inputs

gen double ri = relec + rcomb + rwater + r_inputs
gen double ri_alt = relec + rcomb + rwater + r_inputs_alt

*Labor and Capital Variables
gen double hl = hours_labor_it
gen double nwl = w_total_it
gen double rwl = w_total_it / (nwci / 100)

gen double white_blue_wage_ratio = (w_total_white_c_it / hours_labor_white_c_it) / (w_total_blue_c_it / hours_labor_blue_c_it)
gen double whl = hours_labor_blue_c_it + white_blue_wage_ratio * hours_labor_white_c_it
label variable whl "Weighted Labor Hours"

gen double nk = capital_B_it
gen double rk = nk / (defl_capital_def_stock)  

* Generates the percentage of (real) sales for each product---revenue shares
gen double rho_fjt = rr_fjt / rr_ft

*Creates Product-Specific Inputs--weighted by revenue shares
gen double w_rk = rho_fjt * rk
bys firm_id year: egen double rk_prod = sum(w_rk) 

gen double w_rwl = rho_fjt * rwl
bys firm_id year: egen double rwl_prod = sum(w_rwl) 

gen double w_hl = rho_fjt * hl
bys firm_id year: egen double hl_prod = sum(w_hl) 

gen double w_whl = rho_fjt * whl
bys firm_id year: egen double whl_prod = sum(w_whl) 

gen double w_ri = rho_fjt * ri
bys firm_id year: egen double ri_prod = sum(w_ri)

*Compute export prices for each destination--p_ft is used for single product firms only, and p_fjt is used for multi-product firms
#delimit ;
local countries "AFG AGO AIA ALB ANT ARG ARM ATG AUS AUT AZE BDI BEL BEN BGD BGR BHR 
                   BHS BIH BLR BLZ BMU BOL BRA BRB CAF CAN CHE CHN CIV CMR COD COG 
				   COL CPV CRI CUB CYP CZE DEU DMA DNK DOM DZA ECU EGY ESP EST ETH
				   FIN FJI FLK FRA FSM GAB GBR GEO GHA GIN GMB GNQ GRC GRD GRL GTM 
				   GUY HKG HND HRV HTI HUN IDN IND IRL IRN IRQ ISL ISR ITA JAM JOR
				   JPN KAZ KEN KGZ KHM KIR KNA KOR KWT LAO LBN LBR LBY LCA LKA LTU 
				   LUX LVA MAC MAR MCO MDA MDG MDV MEX MHL MKD MLI MLT MMR MNG MOZ 
				   MRT MUS MWI MYS NAM NER NGA NIC NLD NOR NPL NZL OMN PAK PAN PER 
				   PHL PNG POL PRK PRT PRY QAT ROU RUS SAU SDN SEN SGP SLB SLE SLV 
				   SMR SPM SRB STP SUR SVK SVN SWE SWZ SYC SYR TGO THA TJK TON TTO 
				   TUN TUR TUV TWN TZA UAE UGA UKR URY USA UZB VCT VEN VNM VUT WSM 
				   YEM ZAF ZMB ZWE
				   ABW COK CYM GUM MSR MTQ NCL PRI PYF TCA VGB VIR" ;
#delimit cr
foreach coun of local countries  {
capture gen double p_ft_`coun' = (fob_x_ijtu_`coun' / q_fob_x_ijtu_`coun') / (1 - f1_pct_add_rev_it)
}


foreach coun of local countries  {
capture gen double p_fjt_`coun' = (fob_x_ijtu_`coun' / q_fob_x_ijtu_`coun') / (1 - f1_pct_add_rev_it)
}


*Compute Various Shares for production function estimation code
gen double s_ft_dom = (ni) / (p_ft_dom * y_ft)
/*  s_ft_dom is only defined for single product firms--missing otherwise  */
gen double s_fjt_dom = (ni * rho_fjt) / (p_fjt_dom * y_fjt)
/*  The share of nominal expenditures on intermediate inputs in total nominal output, valued at the domestic price
         multiplied by the product revenue share  */
gen double s_ft = (ni) / (nr_ft)
/*  The standard share at the firm-year level  */


*Generate log versions of several variables
foreach var of varlist s_ft_dom s_fjt_dom s_ft ri hl rwl whl rk p_ft_dom p_fjt_dom rr_ft rr_fjt y_ft y_fjt rho_fjt rk_prod rwl_prod hl_prod whl_prod ri_prod  {
   gen double l_`var' = ln(`var')
   }


save chileall_$ind.dta, replace




